﻿using A.M.ETO;
using L.M.System.Application.IServices;
using L.M.System.Domain.DTO;
using L.M.System.Domain.Entity;
using L.M.System.Infrastruecure.Repository;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using static L.M.System.ErrorCodeEnum.EnumInfor;

namespace L.M.System.Read.API.Controllers
{
    /// <summary>
    /// 单位API
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class Table_of_unitsReadController : ControllerBase
    {
        private readonly ITable_of_unitsServices table_Of_UnitsServices;

        public Table_of_unitsReadController(ITable_of_unitsServices table_Of_UnitsServices = null)
        {
            this.table_Of_UnitsServices= table_Of_UnitsServices;
        }
        /// <summary>
        /// 获取系统配置的按钮(枚举)
        /// </summary>
        /// <returns>获取系统配置的按钮</returns>
        [HttpGet]
        public IActionResult GetEnumStyle()
        {
            ApiReturn<object> apiReturn = new ApiReturn<object>();
            List<object> list = new List<object>();
            foreach (EnumStyle item in Enum.GetValues<EnumStyle>())
            {
                list.Add(new
                {
                    value = item,
                    lable = item.ToString()
                });
            }
            apiReturn.ListInfor = list;
            return Ok(list);
        }

        /// <summary>
        /// 异步分页查询单位表
        /// </summary>
        /// <param name="pageSize">每页条数</param>
        /// <param name="pageIndex">当前也索引</param>
        /// <param name="tName">模糊查询</param>
        /// <returns>异步分页查询单位表</returns>
        [HttpGet]
        public async Task<IActionResult> GetTable_of_unitsAsync(int pageSize, int pageIndex, string? tName)
        {
            var pegeList = await table_Of_UnitsServices.GetTable_of_unitsAsync(pageSize, pageIndex, tName);
            return Ok(pegeList);   
        }

        /// <summary>
        /// 无条件查询全部单位
        /// </summary>
        /// <returns>无条件查询全部</returns>
        [HttpGet]
        public async Task<IActionResult> GetTableUrl()
        {
            var list = await table_Of_UnitsServices.GetShowInforAsync(c=>c .unit_status == true);
            return Ok(list);
        }

        /// <summary>
        /// 单位添加
        /// </summary>
        /// <param name="unit_AddDTO">单位DTO</param>
        /// <returns>统一返回格式</returns>
        [HttpPost]
        public async Task<IActionResult> Addtable_Of_Units(Unit_AddDTO unit_AddDTO)
        {
            var res = await table_Of_UnitsServices.Addtable_Of_Units(unit_AddDTO);
            return Ok(res);
        }

        /// <summary>
        /// 修改单位名称   
        /// </summary>
        /// <param name="unInforUpdDTO">修改的DTO</param>
        /// <returns>统一返回格式</returns>
        [HttpPost]
        public async Task<IActionResult> UpdInforUnInfor(UnInforUpdDTO unInforUpdDTO)
        {
            var res = await table_Of_UnitsServices.UpdInforUnInfor(unInforUpdDTO);
            return Ok(res);
        }


        /// <summary>
        /// 单位u删除
        /// </summary>
        /// <param name="uId">编号</param>
        /// <returns>单位u删除</returns>
        [HttpDelete]
        public async Task<IActionResult> DelInforUnInfor(string? uId)
        {
            ApiReturn<string> apiReturn = new ApiReturn<string>();
            var get = await table_Of_UnitsServices.GetShowObjAsync(c => c.unit_number == uId);
            if (get != null)
            {
                var res = await table_Of_UnitsServices.DelInfor_Async(get);
                if(res > 0)
                {
                    apiReturn.Code = 200;
                    apiReturn.Message = "删除成功";
                }
                else
                {
                    apiReturn.Code = 200;
                    apiReturn.Message = "删除失败";
                }
            }
            return Ok(apiReturn);
        }
    }
}
